package com.yicong.ants.ui.me.address;

import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.cchao.simplelib.core.CollectionHelper;
import com.cchao.simplelib.core.JsonHelper;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.model.event.CommonEvent;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.me.AddressItemBean;
import com.yicong.ants.databinding.AddressListActivityBinding;
import com.yicong.ants.databinding.AddressListItemBinding;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * 地址列表
 *
 * @author cchao
 * @since 2019-12-26
 */
public class AddressListActivity extends BaseTitleBarActivity<AddressListActivityBinding> implements View.OnClickListener {

    @Override
    protected int getLayout() {
        return R.layout.address_list_activity;
    }

    @Override
    protected void initEventAndData() {
        setTitleText("地址管理");
        onLoadData();
        mDataBind.setClick(this);
    }

    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (event.getCode()) {
            case Constants.Event.update_address:
                onLoadData();
                break;
        }
    }

    @Override
    protected void onLoadData() {
        switchView(LOADING);
        addSubscribe(RetrofitHelper.getApis().getAddresses()
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (CollectionHelper.isEmpty(respBean.getData())) {
                    UiHelper.showLongToast("您还没有添加地址，请先添加地址");
                    switchView(CONTENT);
                    RxHelper.timerConsumer(500, new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) throws Exception {
                            Router.turnTo(mContext, AddEditAddressActivity.class).start();
                        }
                    });
                    return;
                }
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    switchView(NET_ERROR);
                    return;
                }
                switchView(CONTENT);
                updateInfo(respBean.getData());
            }, RxHelper.getSwitchErrorConsumer(this)));
    }

    private void updateInfo(List<AddressItemBean> data) {
        mDataBind.linear.removeAllViews();
        for (int i = 0; i < data.size(); i++) {
            int finalI = i;
            AddressListItemBinding itemBinding = DataBindingUtil.inflate(mLayoutInflater, R.layout.address_list_item, mDataBind.linear, false);
            itemBinding.setBean(data.get(i));
            itemBinding.getRoot().setOnClickListener(view -> {
                //
                Router.turnTo(mContext, AddEditAddressActivity.class)
                    .putExtra(Constants.Extra.JSON, JsonHelper.toJson(data.get(finalI)))
                    .start();
            });
            mDataBind.linear.addView(itemBinding.getRoot());
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_address:
                Router.turnTo(mContext, AddEditAddressActivity.class).start();
                break;
        }
    }
}
